extends Node
@export var sum : PackedScene 
@export var player : PackedScene#在根节点，windows节点的检查器上，拖入玩家场景
@export var now_season : int#决定场景布局，0：主菜单

signal return_home
signal summer_end
@onready var windows: Node = $"."
@onready var node: Node = $Node

var su
var pl
var scene_set#链接set_scene文件夹里的资源，里面是各个窗口的属性
var ani#播放玩家角色的动画，大窗口监测点播放走出动画，桥窗口播放走入动画
var now_bridge:Window#当前阶段所用到的桥
var bridge_X:float#桥的总长

#初始化
func _ready() -> void:
	pl = player.instantiate()
	now_season = 0
	scene_state()
	node.connect("Scene_change",_on_node_scene_change)
#用来检测的，组合好游戏吼删掉
func _process(delta: float) -> void:
	#if Input.is_action_just_pressed("p") :
		#print(pl.position)
		#build_bridge(now_bridge,bridge_X)
	#if Input.is_action_just_pressed("h") :
		#now_season += 1
		#scene_state()
	#elif Input.is_action_just_pressed("y") :
		#now_season -= 1
		#scene_state()
	
	if Input.is_action_just_pressed("reset"):
		su.queue_free()
		scene_state()
		
func _on_node_scene_change() -> void:
	print("收到信号")
	now_season += 1
	scene_state()

#根据mow_season判断当前窗口布局，换布局只要让now_season改变然后调用该函数
func scene_state():
	if now_season == 0 :
		get_tree().root.position = Vector2(525,190)
		get_tree().root.size = Vector2(900,700)
		scene_set = load("res://set_scene/season_0.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
		
	if now_season == 1:
		su = sum.instantiate()
		now_bridge = $"1'bridge"
		bridge_X = 269
		$"1'bridge/1'bridge".position = Vector2(265,30)
		$summer/summer_to_main.position = Vector2(3437.504,-202.139)
		get_tree().root.position = Vector2(1647,253)
		get_tree().root.size = Vector2(265,215)
		scene_set = load("res://set_scene/season_1.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
		pl.position = Vector2(-966.0,252.0)
		$summer.add_child(pl)
		$summer.add_child(su)
		Vfs.add_child(pl)
		pl.z_index = 100
		su.connect("res",_on_build_bridge)
		#print("su是否有res信号: ", su.has_signal("res"))
		#var connect_result = su.connect("res",_on_build_bridge)
		#print("信号连接结果: ", "成功" if connect_result == OK else "失败: " + str(connect_result))
		

	if now_season == 2:
		pl.queue_free()
		get_tree().root.position = Vector2(525,190)
		get_tree().root.size = Vector2(900,700)
		scene_set = load("res://set_scene/season_2.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
		
	if now_season == 3:
		now_bridge = $"2'bridge"
		bridge_X = 266
		$summer.borderless = true
		$"2'bridge/2'bridge".position = Vector2(265,35)
		$autumn/autumn_to_main.position = Vector2(900,145)
		get_tree().root.position = Vector2(1234,175)
		get_tree().root.size = Vector2(260,218)
		scene_set = load("res://set_scene/seacon_3.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
		pl = player.instantiate()
		pl.position = Vector2(0,0)
		$autumn.add_child(pl)
		
	if now_season == 4:
		pl.queue_free()
		$summer.borderless = true
		$autumn.borderless = true
		$"3'bridge/3'bridge".position = Vector2(265,35)
		$Winter/winter_to_main.position = Vector2(902,147)
		get_tree().root.position = Vector2(525,190)
		get_tree().root.size = Vector2(900,700)
		scene_set = load("res://set_scene/seacon_4.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
		pl = player.instantiate()
		pl.position = Vector2(0,0)
		$Winter.add_child(pl)

	if now_season == 5:
		now_bridge = $"3'bridge"
		bridge_X = 266
		$summer.borderless = true
		$autumn.borderless = true
		get_tree().root.position = Vector2(1234,175)
		get_tree().root.size = Vector2(260,218)
		scene_set = load("res://set_scene/seacon_5.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)

	if now_season == 6:
		$summer.borderless = true
		$autumn.borderless = true
		$Winter.borderless = true
		get_tree().root.position = Vector2(525,190)
		get_tree().root.size = Vector2(900,700)
		scene_set = load("res://set_scene/seacon_6.tres")
		for i in 8:
			set_window(get_node(scene_set.SetScene_list[i].TheWindow),scene_set.SetScene_list[i].position,scene_set.SetScene_list[i].size,scene_set.SetScene_list[i].visible)
			

#搭桥，输入桥的窗口和桥的总长，每次搭桥三分之一
func _on_build_bridge():
	build_bridge(now_bridge,bridge_X)
	
func build_bridge(target_node:Window,sum:float):
	if target_node.size.x < 3*int(sum/3):
		target_node.position += Vector2i(-sum/3,0)
		target_node.set_size(Vector2i(target_node.get_size()+Vector2i(sum/3,0)))




#设置指定节点的位置，尺寸，是否可见
func set_window(target_node:Node,position:Vector2,size:Vector2,visible:bool):
	target_node.position = position
	target_node.set_size(size)
	if visible:
		target_node.show()
	else :
		target_node.hide()
	pass
	

#以下为平台跳跃切换至桥窗口：
func _on_summer_to_main_body_entered(body: Node2D) -> void:
	summer_end.emit()
	ani = pl.get_node("AnimationPlayer")
	$"summer/summer_to_main/sum-main'Shape".set_deferred("visible",false)
	ani.play("new_animation")
	pl = player.instantiate()
	pl.position = (Vector2(25,35))
	$"1'bridge".add_child(pl)
	pl.get_node("AnimationPlayer").play("new_animation_2")
	

func _on_autumn_to_main_body_entered(body: Node2D) -> void:
	ani = pl.get_node("AnimationPlayer")
	$"autumn/autumn_to_main/aut-main'Shape".set_deferred("visible",false)
	ani.play("new_animation")
	pl = player.instantiate()
	pl.position = (Vector2(25,35))
	$"2'bridge".add_child(pl)
	pl.get_node("AnimationPlayer").play("new_animation_2")

func _on_winter_to_main_body_entered(body: Node2D) -> void:
	ani = pl.get_node("AnimationPlayer")
	$"Winter/winter_to_main/win-main,Shape".set_deferred("visible",false)
	ani.play("new_animation")
	pl = player.instantiate()
	pl.position = (Vector2(25,35))
	$"3'bridge".add_child(pl)
	pl.get_node("AnimationPlayer").play("new_animation_2")

func _on_bridge_body_entered(body: Node2D) -> void:
	now_season += 1
	pl.queue_free()
	return_home.emit()
	scene_state()
	#build_bridge(now_bridge,bridge_X)

	

func _on_sprite_2d_change_the_window() -> void:
	print("111")
	scene_state()
	pass # Replace with function body.
